<?php
/**
 * @file			order.mod.php
 */

class order_mod {

	function order_mod() {
	}

	/**
	 * 首页
	 */
	function default_action()
	{
		$gid = (int)V('g:gid', 0);
		$uid = User::uid();

		if(!$uid) die('please login first.');
		if(!$gid) die('error gid.');

		$ginfo = DR('grouponCom.getGrouponById', '', $gid);
		$ginfo['rst']['finalprice'] = $ginfo['rst']['price'] + $ginfo['rst']['freight'] + $ginfo['rst']['price'] * ($ginfo['rst']['taxrate']/100) ;
        
		TPL :: assign('info', $ginfo['rst']);
		TPL :: display('order');
	}

	/**
	 * 支付
	 */
	function pay()
	{
		$gid = (int)V('g:gid', 0);
		$uid = User::uid();

		if(!$uid) die('please login first.');
		if(!$gid) die('error gid.');

		$ginfo = DR('grouponCom.getGrouponById', '', $gid);
		$ginfo['rst']['finalprice'] = $ginfo['rst']['price'] + $ginfo['rst']['freight'] + $ginfo['rst']['price'] * ($ginfo['rst']['taxrate']/100) ;
        
		TPL :: assign('info', $ginfo['rst']);
		TPL :: display('pay');
	}

	/*
    *  执行订单
    */
	function doOrder()
	{
		$gid = (int)V('p:gid', 0);
		$uid = User::uid();

		if(empty($uid))
		{
			exit(json_encode(array('result'=>0,'data'=>array('msg'=>'请先登录。'))));
		}
		if(empty($gid))
		{
			exit(json_encode(array('result'=>0,'data'=>array('msg'=>'团购信息错误，请重新提交。'))));
		}

		$ginfo = DR('grouponCom.getGrouponById', '', $gid);
		$money = $ginfo['rst']['price'] + $ginfo['rst']['freight'] + $ginfo['rst']['price'] * ($ginfo['rst']['taxrate']/100) ;

		$insertArr = array(
			'userid'=>$uid,
			'goodsid'=>$gid,
			'money'=>$money,
			'count'=>1,
			'state'=>1,
			'time'=> date('Y-m-d H:i:s',APP_LOCAL_TIMESTAMP)
		);
		$result = DR('ordersCom.saveOrdersById','',$insertArr,'');
		if((int)$result['rst'])
		{
			exit(json_encode(array('result'=>1,'data'=>array('url'=>URL('order.pay&gid='.$gid)))));
		}
		else
		{
			exit(json_encode(array('result'=>0,'data'=>array('msg'=>'下订单失败，请重试'))));
		}
	}


	/*
     * 删除
     */
	function del() {
		$id = V('g:id', 0);
		if (!$id) {
			$this->_error('id不能为空', array('search'));
		}
		
		$p = DR('adminCom.getAdminById', '', $this->_getUid());	//获取当前操作者的数据
		if(!$p['rst']['isroot']) {
			$this->_error('您无权限删除', array('search'));
		}

		$rs = DR('ordersCom.delOrders', '', $id);
		if ($rs['rst']) {
			$this->_succ('操作已成功', array('search'));
		}
		
		$this->_error('删除失败',  array('search'));
	}
}
